﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using MediaSage.Lib;
using MediaSage.Lib.Configuration;
using MediaSage.Lib.Database;
using MediaSage.Lib.Plugin;
using MediaSage.Lib.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TVDBProviderTests
{
    [TestClass]
    public class TVDBProviderTests
    {
        public static Config config;
        public static PluginBase provider;

        [ClassInitialize]
        public static void LoadConfiguration(TestContext testContext)
        {            
            config = new Config();
            config.GetAllImages = true;
            config.ImageDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Images");
            Directory.CreateDirectory(config.ImageDir);            
        }

        private static void loadPlugin()
        {
            var PE = new PluginEntity();
            PE.Location = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                                       "TVDBProvider.dll");
            PE.Name = "TVDBProvider";
            provider = config.PluginManager.GetPlugin(PE);
        }

        [TestMethod]
        [DeploymentItem("TVDBProvider.Dll")]
        public void TestGetVersion()
        {
            loadPlugin();
            VersionInfo vi = provider.GetVersionInfo();
            Assert.AreEqual(vi.Name, "TVDBProvider");
            Assert.AreEqual(vi.MajorVersion, 0);
            Assert.AreEqual(vi.MinorVersion, 9);
        }

        [TestMethod]
        [DeploymentItem("TVDBProvider.Dll")]
        public void TestSearchForSeriesByName()
        {
            loadPlugin();
            List<MediaSearchReturn> msr = provider.SearchForMediaByName("Fringe");
            Assert.AreNotEqual(null, msr, "Received null from TVDBProvider.SearchForMediaByName(\"Fringe\")");
            Assert.AreNotEqual(0, msr.Count, "Received 0 results from TVDBProvider.SearchForMediaByName(\"Fringe\")");
        }

        [TestMethod]
        [DeploymentItem("TVDBProvider.Dll")]
        public void TestSearchForSeriesWithYear()
        {
            loadPlugin();
            List<MediaSearchReturn> msr = provider.SearchForMediaByNameAndYear("Chuck", 2007);
            Assert.AreNotEqual(null, msr, "Received null from TVDBProvider.SearchForMediaByName(\"Chuck\",2007)");
            Assert.AreNotEqual(0, msr.Count, "Received 0 results from TVDBProvider.SearchForMediaByName(\"Chuck\",2007)");
            Assert.AreEqual(80348, msr[0].TVDBID, String.Format("Expected TVDBID: 80348 for the top show, got: {0} - {1}", msr[0].TVDBID, msr[0].name));
        }

        [TestMethod]
        [DeploymentItem("TVDBProvider.dll")]
        public void TestGetSeriesWithImages()
        {
            loadPlugin();
            provider.EnableImageDownloads(true);
            MediaClass mc = provider.GetMediaByID(80348);
            Assert.AreNotEqual(null,mc,"Expected to get a result from series download");
            Assert.AreEqual("Chuck",mc.Title,String.Format("Expected title: Chuck, got {0}",mc.Title));
        }

        [TestMethod]
        [DeploymentItem("TVDBProvider.dll")]
        public void TestGetSeriesWithNoImages()
        {
            loadPlugin();
            provider.EnableImageDownloads(true);
            MediaClass mc = provider.GetMediaByID(80348);
            Assert.AreNotEqual(null, mc, "Expected to get a result from series download");
            Assert.AreEqual("Chuck", mc.Title, String.Format("Expected title: Chuck, got {0}", mc.Title));
        }

        [TestMethod]
        [DeploymentItem("TVDBProvider.Dll")]
        public void TestSearchTVDBMovieByMediaObject_IMDBID()
        {
            loadPlugin();
            MediaClass media = new MediaClass(config);
            media.IMDBID = "tt0934814";
            List<MediaSearchReturn> msr = provider.SearchForMedia(media);
            Assert.AreEqual(80348, msr[0].TVDBID, String.Format("Expected 80348 for the TMDBID, got {0}", msr[0].TVDBID));
            Assert.AreEqual(100, msr[0].pctLikelyMatch, "Expected a 100% match for first result");
        }

        [TestMethod]
        [DeploymentItem("TVDBProvider.Dll")]
        public void TestSearchTVDBMovieByMediaObject_TVDBID()
        {
            loadPlugin();
            MediaClass media = new MediaClass(config);
            media.TvdbID = 80348;
            List<MediaSearchReturn> msr = provider.SearchForMedia(media);
            Assert.AreEqual("tt0934814", msr[0].IMDBID,
                            String.Format("Expected tt0934814 for the IMDBID, got {0}", msr[0].IMDBID));
            Assert.AreEqual(100, msr[0].pctLikelyMatch, "Expected a 100% match for first result");
        }

        [TestMethod]
        [DeploymentItem("TVDBProvider.Dll")]
        public void TestSearchTVDBMovieByMediaObject_TitleAndDate()
        {
            loadPlugin();
            MediaClass media = new MediaClass(config);

            media.Title = "Chuck";
            media.Released = new DateTime(2011, 1, 1);            
            List<MediaSearchReturn> msr = provider.SearchForMedia(media);
            Assert.AreEqual(80348, msr[0].TVDBID, String.Format("Expected 80348 for TVDBID, got {0}", msr[0].TVDBID));

        }

        [TestMethod]
        [DeploymentItem("TVDBProvider.Dll")]
        public void TestSearchTVDBMovieByMediaObject_Title()
        {
            loadPlugin();
            MediaClass media = new MediaClass(config);

            media.Title = "Chuck";
            List<MediaSearchReturn> msr = provider.SearchForMedia(media);
            Assert.AreEqual(80348, msr[0].TVDBID, String.Format("Expected 80348 for TVDBID, got {0}", msr[0].TVDBID));
        }

        [TestMethod]
        [DeploymentItem("TVDBProvider.zip")]
        public void TestInstallTVDBProvider()
        {            
            var installer = new Installer(config);          

            bool result=installer.InstallPlugin("TVDBProvider.zip",false);
            Assert.AreEqual(true,result,"Install TVDBProvider failed");
            PluginBase TVDBProvider = config.PluginManager.GetPluginByName("TVDBProvider");
            VersionInfo vi = TVDBProvider.GetVersionInfo();
        }
    }
}